package com.ithe.canteen.controller;

import com.ithe.canteen.common.Result;
import com.ithe.canteen.pojo.Collect;
import com.ithe.canteen.service.CollectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/collect")
public class CollectController {

    @Autowired
    private CollectService collectService;

    @PostMapping("/addCollect")
    public Result addCollect(@RequestBody Collect collect) {
        String foodID = collect.getCollectFood();
        String customerID = collect.getCollectUser();
        Integer number = collectService.addCollect(foodID, customerID);
        if (number == 1) {
            return Result.success(1);
        } else if (number == 2) {
            return Result.error("400", "菜品已经收藏");
        }
        return Result.error("菜品收藏失败");
    }

    @PostMapping("/deleteCollect")
    public Result deleteCollect(@RequestBody Collect collect) {
        String foodID = collect.getCollectFood();
        String customerID = collect.getCollectUser();
        Integer number = collectService.deleteCollect(foodID, customerID);
        if (number == 1) {
            return Result.success(1);
        } else if (number == 2) {
            return Result.error("404", "此菜品未收藏");
        }
        return Result.error("取消收藏失败");
    }

    @PostMapping("/getCollectState")
    public Result getCollectState(@RequestBody Collect collect) {
        String foodID = collect.getCollectFood();
        String customerID = collect.getCollectUser();
        return Result.success(collectService.getCollectState(foodID, customerID));
    }

    @PostMapping("/getCollectList")
    public Result getCollectList(@RequestBody Collect collect) {
        String customerID = collect.getCollectUser();
        return Result.success(collectService.getCollectList(customerID));
    }
}
